package com.kitty.game.rank.model.monitor;

import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.common.utils.SpringUtils;
import com.kitty.listener.EventDispatcher;
import com.kitty.listener.EventType;
import com.kitty.listener.event.*;
import com.kitty.game.fight.service.CountService;

/**宠物属性监听*/
public class PetAttribMonitor {
    private Role role;
    private Pet pet;

    private int phyPower;//物理伤害
    private int magPower;//法术伤害
    private int defense;//防御
    private int speed;//速度


    public PetAttribMonitor(Role role, Pet pet) {
        this.role = role;
        this.pet = pet;

        /**先存储当前值*/
        CountService countService = SpringUtils.getBean(CountService.class);
        this.phyPower = countService.countPetMaxPhyPower(pet);
        this.magPower = countService.countPetMaxMagPower(pet);
        this.defense = countService.countPetMaxDefense(pet);
        this.speed = countService.countPetMaxSpeed(pet);
    }

    /**监听结束*/
    public void end() {
        /**对比当前值与之前值是否有差异，有差异时产生对应的改变事件*/
        boolean falg = false;
        CountService countService = SpringUtils.getBean(CountService.class);
        int currentPhyPower = countService.countPetMaxPhyPower(pet);
        if (currentPhyPower != phyPower) {
            falg = true;
            /** 产生一个宠物物理伤害改变事件 */
            EventDispatcher.getInstance().fireEvent(new PetPhyPowerChangeEvent(EventType.PET_PHY_POWER_CHANGE, role, pet, currentPhyPower));
        }
        int currentMagPower = countService.countPetMaxMagPower(pet);
        if (currentMagPower != magPower) {
            falg = true;
            /** 产生一个宠物法术伤害改变事件 */
            EventDispatcher.getInstance().fireEvent(new PetMagPowerChangeEvent(EventType.PET_MAG_POWER_CHANGE, role, pet, currentMagPower));
        }
        int currentDefense = countService.countPetMaxDefense(pet);
        if (currentDefense != defense) {
            falg = true;
            /** 产生一个宠物防御改变事件 */
            EventDispatcher.getInstance().fireEvent(new PetDefenseChangeEvent(EventType.PET_DEFENSE_CHANGE, role, pet, currentDefense));
        }
        int currentSpeed = countService.countPetMaxSpeed(pet);
        if (currentSpeed != speed) {
            falg = true;
            /** 产生一个速度改变事件 */
            EventDispatcher.getInstance().fireEvent(new PetSpeedChangeEvent(EventType.PET_SPEED_CHANGE, role, pet, currentSpeed));
        }
        if(falg){
            int maxLife = countService.countPetMaxLife(pet);
            int maxMana = countService.countPetMaxMana(pet);
            int l_zhanli = phyPower+magPower+speed+defense+maxLife+maxMana/2;
            /** 产生一个战力改变事件 */
            EventDispatcher.getInstance().fireEvent(new PetZhanLiChangeEvent(EventType.PET_ZHANLI_CHANGE, role, pet, l_zhanli));
        }
    }
}
